package com.azunitech.jms.ejb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;

import com.azunitech.jms.common.EventMessage;
import com.azunitech.jms.common.IEventRepository;
import com.google.inject.Inject;


@MessageDriven(mappedName="Queue-001-event",
		activationConfig = { @ActivationConfigProperty(
				propertyName = "destinationType", propertyValue = "javax.jms.Queue")
		})
public class EventProcessorMDB implements MessageListener {
	private Logger s_Logger = Logger.getLogger(EventProcessorMDB.class);
	@Inject IEventRepository EventRepository;

    public EventProcessorMDB() {
    }
	
    public void onMessage(Message message) {
    	s_Logger.info(message);
		try {
	    	if (message instanceof ObjectMessage){
				ObjectMessage objMsg = (ObjectMessage)message;
				Object msgObject;
				msgObject = objMsg.getObject();
				if ( msgObject instanceof EventMessage){
					EventMessage msg = (EventMessage)msgObject;
			    	s_Logger.info(message);
					//EventRepository.process(msg);
				}
			}
		}
    	catch (JMSException e) {
			e.printStackTrace();
		}
    }
}
